package com.hxd.otc.main;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hxd.otc.R;
import com.hxd.otc.api.Constant;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.GlideUtils;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.widget.BlocksViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import io.rong.photoview.PhotoView;
import io.rong.photoview.PhotoViewAttacher;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/9 11:54
 * 描  述 :  ${图片预览Activity}
 */
public class ImagePreviewActivity extends BaseActivity {

    @BindView(R.id.preview_bvp)
    BlocksViewPager mPreviewPager;
    @BindView(R.id.preview_rg)
    RadioGroup mPreviewRadioGroup;
    private ArrayList<String> mImagePathData;
    private PreviewPagerAdapter mAdapter;
    private int mCheckPosition;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_image_preview;
    }

    @Override
    protected void init() {
        setFullWindows(true);
        super.init();
    }

    @Override
    protected void initView() {
        /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setEnterTransition(new Fade().setDuration(1000));
        }*/
    }

    @Override
    protected void initData() {

        Intent intent = getIntent();
        mImagePathData = intent.getStringArrayListExtra(Constant.Order.PREVIEW_IMAGE_PATH_KEY);
        mCheckPosition = intent.getIntExtra(Constant.Order.PREVIEW_IMAGE_POSITION, 0);
        initRadioButton();
        LogUtils.w("initData--", mImagePathData.size() + "");
        if (mAdapter == null) {
            mAdapter = new PreviewPagerAdapter();
            mPreviewPager.setAdapter(mAdapter);
            mPreviewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    mCheckPosition = position;
                    checkButton(position);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        } else {
            mAdapter.notifyDataSetChanged();
        }
        mPreviewPager.setCurrentItem(mCheckPosition);
    }

    private void initRadioButton() {
        if (mImagePathData != null && mImagePathData.size() > 1) {
            for (int i = 0; i < mImagePathData.size(); i++) {
                RadioButton radioButton = new RadioButton(this);
                radioButton.setClickable(false);
                if (i == 0) {
                    radioButton.setChecked(true);
                }
                mPreviewRadioGroup.addView(radioButton, i);
                radioButton.setButtonDrawable(null);
                radioButton.setBackgroundResource(R.drawable.selector_pager_radio_view);
                //LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(DataUtils.dp2px(this,10),DataUtils.dp2px(this,10));
                RadioGroup.LayoutParams layoutParams = (RadioGroup.LayoutParams) radioButton.getLayoutParams();
                layoutParams.width = DataUtils.dp2px(this, 8);
                layoutParams.height = DataUtils.dp2px(this, 8);
                layoutParams.setMargins(DataUtils.dp2px(this, 20), 0, 0, 0);
                radioButton.setLayoutParams(layoutParams);
                //radioButton.setPadding(DataUtils.dp2px(this, 10), DataUtils.dp2px(this, 10), DataUtils.dp2px(this, 10), DataUtils.dp2px(this, 10));
            }
            checkButton(mCheckPosition);
        }
    }

    private void checkButton(int position) {
        if (mPreviewRadioGroup.getChildCount() > 0 && position < mPreviewRadioGroup.getChildCount()) {
            for (int i = 0; i < mPreviewRadioGroup.getChildCount(); i++) {
                RadioButton radioButton = (RadioButton) mPreviewRadioGroup.getChildAt(i);
                radioButton.setChecked(i == position);
            }
        }
    }


    @Override
    protected void initEvent() {
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }


    public class PreviewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImagePathData == null ? 0 : mImagePathData.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @Override
        public int getItemPosition(@NonNull Object object) {
            return POSITION_NONE;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//            super.destroyItem(container, position, object);
            container.removeView((View) object);
        }

        @NonNull
        @Override
        public View instantiateItem(@NonNull ViewGroup container, int position) {
            View inflate = LayoutInflater.from(ImagePreviewActivity.this).inflate(R.layout.item_iamge_preview, container, false);
            container.addView(inflate);
            PhotoView photoView = inflate.findViewById(R.id.photo_iv);
            GlideUtils.setImage(ImagePreviewActivity.this, mImagePathData.get(position), photoView);
            LogUtils.w("initData---", mImagePathData.get(position));
            photoView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
                @Override
                public void onViewTap(View view, float x, float y) {
                    finish();
                }
            });
            /*PhotoView photoView = new PhotoView(ImagePreviewActivity.this);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            photoView.setLayoutParams(layoutParams);
            GlideUtils.setImage(ImagePreviewActivity.this, mImagePathData.get(position), photoView);
            LogUtils.w("initData---", mImagePathData.get(position));
            photoView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
                @Override
                public void onViewTap(View view, float x, float y) {
                    finish();
                }
            });
            container.addView(photoView);
            return photoView;*/
            return inflate;
        }

    }

}
